<%@ page import="java.util.*,merlin.beans.*,merlin.dao.*,merlin.managers.*,merlin.servlets.*,merlin.utils.*" errorPage="mr_errhandler.jsp" %>

<jsp:useBean id="iwRegBean" class="merlin.beans.IWRegistrationBean" scope="session"/>
<jsp:setProperty name="iwRegBean" property="*"/>


<%
	ApplicationResources resource = ApplicationResources.getInstance();

	String separatorString = resource.getProperty("separatorString");
	String separatorPattern = resource.getProperty("separatorPattern");
%>

<HTML>
<HEAD>
    <TITLE>Search Reports</TITLE>
    <script>

		function isBlank(str){
			var len = str.length;
			if(len==0)return true;	// zero-length string
			for(i=0;i<len;i++){
				if(str.charAt(i) !=' ' && str.charAt(i) !='\n')return false; //some character is there
			}
			return true;
		}

		function transferCompany(lstFrom,lstTo,isDuplicacyChecked){
			try{
				var index = lstFrom.selectedIndex;
				if(index == -1){
					alert("Select a company to add");
					return;
				}
				// check if the company already exists in the selected companies list.
				var exists = false;
				if(isDuplicacyChecked){
					var valCompany = lstFrom.options[index].value;
					var lstTolen = lstTo.length;
					for(j=0;j<lstTolen;j++){
						if(valCompany == lstTo.options[j].value){
							alert("Company already selected");
        					exists = true;
		       				lstFrom.options[index].selected = false;
							break;
	    		    	}
    	   			}
    	   		}
		        if(! exists){
					// add the company to the list
	    		    var aOption = document.createElement("OPTION");
					aOption.text = lstFrom.options[index].text;
					aOption.value = lstFrom.options[index].value;
					// browser specific addition, different in Mac Safari.
					if(navigator.userAgent.indexOf("Safari") > -1){
						// Mac Safari
						lstTo.add(aOption);
					}else{
						// other browsers.
						lstTo.options.add(aOption);
					}

					// and remove it from the other
					lstFrom.remove(index);

                    // sort the data.
					sortSelect(lstTo);
				}
			}catch(e){
				alert("transferCompany : " + e.message);
			}
		}
        	// sort function - ascending (case-insensitive)
        function sortFuncAsc(record1, record2) {
	        var value1 = record1.optText.toLowerCase();
    	    var value2 = record2.optText.toLowerCase();
        	if (value1 > value2) return(1);
            if (value1 < value2) return(-1);
	        return(0);
    	}

	    // sort the select object in ascending order.
		function sortSelect(selectToSort) {
			try{
				// copy options into an array
			    var myOptions = new Array();
				for (var loop=0; loop<selectToSort.options.length; loop++) {
					myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
				}

				// sort array
				myOptions.sort(sortFuncAsc);

				// copy sorted options from array back to select box
				selectToSort.options.length = 0;

				// browser identification
				var isSafari =	(navigator.userAgent.indexOf("Safari") > -1)? true: false;

				for (var loop=0; loop<myOptions.length; loop++) {
					var optObj = document.createElement('option');
					optObj.text = myOptions[loop].optText;
					optObj.value = myOptions[loop].optValue;
					if(isSafari){
						selectToSort.add(optObj);
					}else{
						selectToSort.options.add(optObj);
					}
				}
			}catch(e){
				alert("sortSelect : " + e.message);
			}
		}

		function radOrderMethodClick(frm){
			if (frm.lstSelectedReports.options.length > 0)
				frm.btnYourDetails.disabled = false;
		}

		function submitFormForSearch(frm){
			// sanity check
			if(isBlank(frm.txtSearch.value)){
				alert("Enter search text");
				frm.txtSearch.focus();
				return false;
			}

			var reportTitles ="";
			var lstLen = frm.lstSelectedReports.length;
			if(lstLen> 0){
   				for(i=0;i < lstLen - 1;i++){
					reportTitles += frm.lstSelectedReports.options[i].value + "<%= separatorString %>" + frm.lstSelectedReports.options[i].text + "<%= separatorString %>";
				}
				reportTitles += frm.lstSelectedReports.options[lstLen-1].value + "<%= separatorString %>" + frm.lstSelectedReports.options[lstLen-1].text;
			}
			frm.hidSelectedReports.value=reportTitles;
			frm.mode.value="searchReportsByReportDesc";
			frm.action = "merlin";
			frm.submit();
			return true;
		}

		function submitFormForDetails(frm){
			// sanity check

			// find if any of the radio buttons are checked or not.
			if(frm.radOrderMethod[0].checked == false && frm.radOrderMethod[1].checked == false){
				alert("You must select one mode of providing your details");
				return;
			}
            // find if any of the reports is being selected or not.
			if(frm.lstSelectedReports.length==0){
				alert("You must select at least one report before you can continue to this page.");
				return;
			}

			var reportTitles ="";
			var lstLen = frm.lstSelectedReports.length;
			if(lstLen> 0){
   				for(i=0;i < lstLen - 1;i++){
					reportTitles += frm.lstSelectedReports.options[i].value + "<%= separatorString %>" + frm.lstSelectedReports.options[i].text + "<%= separatorString %>";
				}
				reportTitles += frm.lstSelectedReports.options[lstLen-1].value + "<%= separatorString %>" + frm.lstSelectedReports.options[lstLen-1].text;
			}
			frm.hidSelectedReports.value=reportTitles;

			//determine the order method and redirect accordingly
			if(frm.radOrderMethod[0].checked){
				// Contact Details
				frm.action="mr_provideContactDetails.jsp";
			}else{
				// payment Details
				frm.action = "mr_reportSelection.jsp";
			}
  			frm.submit();
			return true;
		}
	</script>
</HEAD>
<BODY onload="document.frmSearch.txtSearch.focus()">

	<jsp:include page="mr_header.jsp"/>
	<p/>
	<table align="center" width="750px" border="0">
		<tr>
			<td colspan="2" align="center" style='color:white;height:23px;font: 11px verdana,arial, sans-serif ;background:url("mr_images/LongBars-orange750.jpg") no-repeat;' >
				<h5 style="vertical-align:middle">Search</h5>
			</td>
		</tr>
	</table>
	<center>
		<div>
			<form action="merlin" name="frmSearch" method="post" onsubmit=" return submitFormForSearch(this)">

				<input type="hidden" name="mode" value="searchReportsByReportDesc">
				<input type="hidden" name="hidSelectedReports" value="">
				<input type="hidden" name="pageName" value="mr_searchReports.jsp">
				<input type="hidden" name="emailFrom" value="Website Search">

				<table style="width: 750px;	font-family: Verdana, Helvetica, Arial, sans-serif;font-size: 11px;">
					<tbody>
						<tr>
							<td colspan="2"><span class="boldGreen">Step 1</span><br>Search by a keyword in the title (eg &quot;INSURANCE&quot; for all reports on Insurance Intermediaries) or <a href="enquiries.jsp">view all report titles here</a>.</td>
						</tr>
						<tr>
							<td colspan="2"><input name="txtSearch" style="text-transform: uppercase;" value="" type="text"> <input name="btnSearch" class="input" value="Search For Report" style="font-size: 11px; width: 130px;"  type="submit"></td>            <!-- onclick="submitFormForSearch(this.form)" -->
						</tr>
						<tr>
							<td style="padding: 18px 0px 0px; width: 50%;">
								<span class="boldGreen">Step 2</span>
								<br>The reports listed here were found by your search. 								<em><br>(Click on a report title to select those of potential interest)</em>.
							</td>
							<td style="padding: 33px 0px 0px;">The reports listed here are the titles you have selected.<em><br>(Click to remove a report)</em>.
							</td>
						</tr>
						<tr>
							<td>
								<select name="lstSearchedReports" multiple size="5" onchange="transferCompany(this,this.form.lstSelectedReports,true)" style="width: 300px;">
						  		<%
						     		DbResultSet dbRes = (DbResultSet)request.getAttribute("reportDetails");
						     		if(dbRes != null){
						     			for(int i=0;i<dbRes.size();i++){
						     				ReportTitlesBean bean = (ReportTitlesBean)dbRes.get(i);
											out.println("<option value=\"" + bean.getReportTitlesId() + "\">" + bean.getReportTitle() + "</option>");
						     			}
							   		}
								%>
								</select>
							</td>
							<td>
								<select name="lstSelectedReports" multiple size="5" onchange="transferCompany(this,this.form.lstSearchedReports,false)" style="width: 300px;">
						  		<%
						  			// look for "hidSelectedReports" in both request.getAttribute() & request.getParameter()
						  			String selectedReports = request.getParameter("hidSelectedReports");
						  			if(selectedReports == null){
						  				selectedReports = (String) request.getAttribute("hidSelectedReports");
						  			}
						  			if(selectedReports != null && selectedReports.length() > 0){
								  		String array[] = selectedReports.split(separatorPattern);
								  		int tokens = array.length;
						  				for(int i=0;i<tokens - 1;i+=2){
								  			String reportNumber = array[i];
								  			String reportName = array[i+1];
						             		out.println("<option value=\"" + reportNumber + "\">" + reportName + "</option>");
								  		}
						        	}
						  		%>
								</select>
							</td>
						</tr>
						<tr>
							<td colspan="2" style="padding: 18px 0px 0px;">
								<span class="boldGreen">Step 3</span>
							</td>
						</tr>
						<tr>
							<td colspan="2">
								<input name="radOrderMethod" value="S" type="radio">Send me order forms for the reports I have selected showing the companies analysed<br>
    							<input name="radOrderMethod" value="C" type="radio">I wish to purchase the reports I have selected using my credit card						</td>
   						</tr>
   						<tr>
    						<td colspan="2"><input name="btnYourDetails" class="input" value="Your Details" onclick="submitFormForDetails(this.form)" style="font-size: 11px; width: 130px;" type="button">
							</td>
						</tr>
					</tbody>
				</table>
  			</form>
		</div>
	</div>
	<center>
	<jsp:include page="mr_footer.jsp"/>

	<!-- Start of StatCounter Code
<script type="text/javascript" language="javascript">
var sc_project=928985;
var sc_invisible=1;
var sc_partition=7;
var sc_security="206963a7";
</script>

<script type="text/javascript" language="javascript" src="http://www.statcounter.com/counter/frames.js"></script><noscript><a href="http://www.statcounter.com/" target="_blank"><img  src="http://c8.statcounter.com/counter.php?sc_project=928985&amp;amp;java=0&amp;amp;security=206963a7&amp;amp;invisible=1" alt="hidden hit counter" border="0"></a> </noscript>
End of StatCounter Code -->

</BODY>
</HTML>